package com.config.exception;

import com.auth0.jwt.exceptions.InvalidClaimException;
import org.apache.ibatis.binding.BindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.management.OperationsException;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class GlobalException {
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Map<String, Object> exceptionHandle(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code" , "-502");
        result.put("msg" , "服务器异常");
        return result;
    }

    @ExceptionHandler(OperationsException.class)
    @ResponseBody
    public Map<String, Object> operationsException(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code" , "-503");
        result.put("msg", "权限不足");
        return result;
    }


    @ExceptionHandler(BindingException.class)
    @ResponseBody
    public Map<String, Object> BindingException(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code" , "-504");
        result.put("msg" , "数据为空");
        return result;
    }

    @ExceptionHandler(InvalidClaimException.class)
    @ResponseBody
    public Map<String, Object> InvalidClaimException(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code" , "-505");
        result.put("msg" , "token过期");
        return result;
    }

}
